<template>
  <div>
    <FaPageMain>
      <!--    搜索框-->
      <div style="margin: 10px 0">
        <el-form :inline="true" :model="selectForm" class="demo-form-inline">
          <el-form-item label="名称">
            <el-input v-model="selectForm.userName" placeholder="请输入用户名" clearable />
          </el-form-item>
          <el-form-item label="角色分类">
            <el-select v-model="selectForm.userRole" placeholder="请选择角色分类"  clearable>
              <el-option label="管理员" value="admin" />
              <el-option label="普通用户" value="user" />
              <el-option label="封禁" value="ban" />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="list">查询</el-button>
          </el-form-item>
        </el-form>
      </div>

      <el-table :data="tableData" style="width: 100%">
        <el-table-column prop="id" label="编号"/>
        <el-table-column prop="userAccount" label="账号"/>
        <el-table-column prop="userName" label="名称"/>
        <el-table-column
          prop="userRole"
          label="角色"
          width="100"
        >
          <template #default="scope">
            <el-tag
              effect="light"
              :type="getTypeAndTextByStatus(scope.row.userRole).type">{{ getTypeAndTextByStatus(scope.row.userRole).text }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="createTime" label="创建时间"/>
        <el-table-column prop="updateTime" label="更新时间"/>
        <el-table-column fixed="right" label="操作" width="200">
          <template #default="scope">
            <el-button type="primary" size="small" @click="edit(scope.row)">编辑</el-button>
            <el-button type="primary" size="small" @click="del(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="example-pagination-block">
        <el-pagination
          v-model:current-page="current"
          v-model:page-size="size"
          :page-sizes="[5, 10, 20, 50]"
          :size="size"
          :disabled="disabled"
          :background="background"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </FaPageMain>

    <el-dialog v-model="dialogVisible" title="编辑用户" width="30%" draggable>
      <el-form :model="editForm" label-width="auto" style="max-width: 600px; margin-top: 10px">
        <el-form-item label="用户账号">
          <el-input v-model="editForm.userAccount" disabled/>
        </el-form-item>
        <el-form-item label="用户名">
          <el-input v-model="editForm.userName" />
        </el-form-item>
        <el-form-item label="权限">
          <el-select v-model="editForm.userRole" placeholder="please select your role">
            <el-option label="管理员" value="admin" />
            <el-option label="普通用户" value="user" />
            <el-option label="封禁" value="ban" />
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="confirm(editForm)">
            确定
          </el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script lang="js">
import api from '@/api'
import {toast} from "vue-sonner";

export default {
  created() {
    this.list()
  },
  data() {
    return {
      tableData: [],
      current: 1,
      size: 5,
      total: 50,
      status: undefined,
      dialogVisible: false,
      selectForm: {
        userName: undefined,
        userRole: undefined
      },
      editForm: {
        id: undefined,
        username: undefined,
        userAccount: undefined,
        userRole: undefined
      }
    }
  },
  methods: {
    list() {
      const pageParams = {
        current: this.current,
        pageSize: this.size,
        status: this.status,
        ...this.selectForm
      }
      api.post("/user/list/page", pageParams).then(res => {
        console.log(res)
        this.tableData = res.data.records
        this.size = res.data.size
        this.current = res.data.current
        this.total = res.data.total
      })
    },
    del(val) {
      api.post("/user/delete", {
        id: val.id
      }).then(res => {
        console.log(res)
        if (res.code === 0) {
          this.list()
          toast.success(`成功删除编号为${val.id}的用户！`)
        } else {
          toast.error(`编号:${val.id}用户删除失败！`)
        }
      })
    },
    edit(val) {
      this.dialogVisible = true
      this.editForm.id = val.id
      this.editForm.userName = val.userName
      this.editForm.userAccount = val.userAccount
      this.editForm.userRole = val.userRole
      console.log('xx', this.editForm)
    },
    confirm() {
      api.post("/user/update", this.editForm).then(res => {
        console.log(res)
        if (res.code === 0) {
          this.list()
          toast.success(`成功更新编号为${this.editForm.id}的用户！`)
        } else {
          toast.error(`编号:${this.editForm.id}}用户编辑失败！`)
        }
      })
      this.dialogVisible = false
    },
    handleSizeChange(val) {
      this.pageSize = val
      this.list()
    },
    handleCurrentChange(val) {
      this.current = val
      this.list()
    },
    getTypeAndTextByStatus(status) {
      if (status === 'admin') {
        return {
          type: 'success',
          text: '管理员'
        }
      }
      if (status === 'user') {
        return {
          type: 'warning',
          text: '普通用户'
        }
      }
      if (status === 'ban') {
        return {
          type: 'danger',
          text: '封禁'
        }
      }
    }
  }
}
</script>

<style scoped>
.example-pagination-block {
  margin-top: 10px;
}

.demo-form-inline .el-input {
  --el-input-width: 220px;
}

.demo-form-inline .el-select {
  --el-select-width: 220px;
}
</style>
